<?php

namespace App\Modules\Apicrzs\Http\Controllers\Public;

use App\Library\Wechat\JssdkService;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsFreeModel;
use App\Modules\Apicrzs\Http\Controllers\BaseApicrzsController;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class EquipmentController extends BaseApicrzsController
{
    public function getSource(Request $request)
    {
        $params = $request->all();
        $water_id = $params['eid'];
        $ttype = $params['type'];

        if(Cache::has('water:device:equipment_params:'.$water_id.':'.$ttype)){
            $fee_info = Cache::get('water:device:equipment_params:water:'.$water_id.':'.$ttype);
            $equipment_id = $fee_info['equipment_id'];
            $equipment_info = Cache::get('water:device:job_cache:eq_all:'.$equipment_id);
        }else {
            //$fee_water_info = EquipmentParamsFreeModel::getInstance()->getWaterDeviceIdByType($params['eid'],$params['type']);
            $fee_water_info = EquipmentParamsFreeModel::getInstance()->getEquipmentParamsFreeCache();
            $fee_info = $fee_water_info['water'][$water_id][$ttype];
        }
        $browser_type = IsBrowserType();
        xdebug($browser_type,'扫码终端','browser');
        if (is_alipay()) {
            return view('alipay',[
                'deviceId'=>$params['eid'],
                'type'=>$params['type'],
                'fee_id'=>$fee_info['id'],
                'water_info'=>$fee_info,
                'water_title'=>$equipment_info['water_title'],
                'openid'=>'',
                'msg'=>'暂不支持【支付宝】',
            ]);
        }elseif (is_weixin()){
            $jssdk = new JssdkService();
            $signPackage = $jssdk->GetSignPackage();
            return view('wx',[
                'deviceId'=>$params['eid'],
                'type'=>$params['type'],
                'fee_id'=>$fee_info['id'],
                'openid'=>'',
                'signPackage'=>$signPackage,
            ]);
        }else{
            return view('browser',[
                'deviceId'=>$params['eid'],
                'type'=>$params['type'],
                'fee_id'=>$fee_info['id'],
                'openid'=>'','msg'=>'请使用微信或者支付宝扫码']);
        }
    }
    public function getAlipaySource(Request $request)
    {
        $params = $request->all();
        $water_id = $params['eid'];
        $ttype = $params['type'];

        if(Cache::has('water:device:equipment_params:'.$water_id.':'.$ttype)){
            $fee_info = Cache::get('water:device:equipment_params:water:'.$water_id.':'.$ttype);
            $equipment_id = $fee_info['equipment_id'];
            $equipment_info = Cache::get('water:device:job_cache:eq_all:'.$equipment_id);
        }else {
            //$fee_water_info = EquipmentParamsFreeModel::getInstance()->getWaterDeviceIdByType($params['eid'],$params['type']);
            $fee_water_info = EquipmentParamsFreeModel::getInstance()->getEquipmentParamsFreeCache();
            $fee_info = $fee_water_info['water'][$water_id][$ttype];
        }
        $browser_type = IsBrowserType();
        xdebug($browser_type,'扫码终端','browser');

        return view('alipaycs',[
            'deviceId'=>$params['eid'],
            'type'=>$params['type'],
            'fee_id'=>$fee_info['id'],
            'water_info'=>$fee_info,
            'water_title'=>$equipment_info['water_title'],
            'openid'=>'',
            'msg'=>'暂不支持【支付宝】',
        ]);
    }

    public function getAlipay(Request $request)
    {
        if (in_array(IsBrowserType(),['Alipay','WeChat'])){
            return $this->sendApiError('对不起，操作失败');
        }
        $jssdk = new JssdkService();
        $signPackage = $jssdk->GetSignPackage();
        $params = $request->all();
        return view('alipay',[
            'deviceId'=>$params['eid'],
            'type'=>$params['type'],
            'openid'=>'',
            'signPackage'=>$signPackage,
        ]);
    }

}
